<?php
namespace app\common\controller;

use org\Auth;
use think\Loader;
use think\Cache;
use think\Controller;
use think\Db;
use think\Session;

/**
 * 后台公用基础控制器
 * Class AdminBase
 * @package app\common\controller
 */
class AdminBase extends Controller
{
    protected function _initialize()
    {
        parent::_initialize();

        $this->checkAuth();
        $this->getMenu();

        // 输出当前请求控制器（配合后台侧边菜单选中状态）
        $this->assign('controller', Loader::parseName($this->request->controller()));
    }

    /**
     * 权限检查
     * @return bool
     */
    protected function checkAuth()
    {

        if (!Session::has('admin_id')) {
            $this->redirect('admin/login/index');
        }

        $module     = $this->request->module();
        $controller = $this->request->controller();
        $action     = $this->request->action();

        // 排除权限
        $not_check = ['admin/Index/index', 'admin/AuthGroup/getjson', 'admin/System/clear'];

        if (!in_array($module . '/' . $controller . '/' . $action, $not_check)) {
            $auth     = new Auth();
            $admin_id = Session::get('admin_id');
            if (!$auth->check($module . '/' . $controller . '/' . $action, $admin_id) && $admin_id != 2) {
                $this->error('没有权限');
            }
        }
    }

    /**
     * 获取侧边栏菜单
     */
    protected function getMenu()
    {
        $menu     = [];
        $admin_id = Session::get('admin_id');
        $auth     = new Auth();

        $auth_rule_list = Db::name('auth_rule')->where('status', 1)->order(['sort' => 'DESC', 'id' => 'ASC'])->select();

        foreach ($auth_rule_list as $value) {
            if ($auth->check($value['name'], $admin_id) || $admin_id == 2) {
                $menu[] = $value;
            }
        }
        $menu = !empty($menu) ? array2tree($menu) : [];

        $this->assign('menu', $menu);
    }

    /**
     * 显示状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function toggle1($ids, $type,$modelname)
    {
        $data   = [];
        $status = $type == 'audit' ? 1 : 0;
        if (!empty($ids)) {
            foreach ($ids as $value) {
                $data[] = ['id' => $value, 'status' => $status];
            }
            if ($this->$modelname->saveAll($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('请选择需要操作的案例');
        }
    }

    /**
     * 置顶状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function zhiding1($ids, $type,$modelname)
    {
        $data   = [];
        $status = $type == 'audit' ? 1 : 0;
        if (!empty($ids)) {
            foreach ($ids as $value) {
                $data[] = ['id' => $value, 'top' => $status];
            }
            if ($this->$modelname->saveAll($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('请选择需要操作的案例');
        }
    }


        /**
     * 置顶状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function hot1($ids, $type,$modelname)
    {
        $data   = [];
        $status = $type == 'audit' ? 1 : 0;
        if (!empty($ids)) {
            foreach ($ids as $value) {
                $data[] = ['id' => $value, 'hot' => $status];
            }
            if ($this->$modelname->saveAll($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('请选择需要操作的案例');
        }
    }

    /**
     * 删除操作
     * @param int   $id
     * @param array $ids
     */
    public function delete1($id, $ids)
    {
        if($ids){           
            $i = 0;
            for(;$i<count($ids);$i++){
                $list = $this->nav_news->deletephoto($ids[$i]);
            }
            $id = array_merge($ids);
            $this->nav_news->destroy($id);
            $this->success('删除成功');

        }

        if ($id) {
            $list = $this->nav_news->deletephoto($id);
            if ($this->nav_news->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的文章');
        }
    }

     /**
     * 删除操作(删除图片和缩略图)
     * @param int   $id
     * @param array $ids
     */
    public function delete2($id, $ids,$modelname)
    {
        if($ids){           
            $i = 0;
            for(;$i<count($ids);$i++){
                $list[]  = $this->deletephoto($ids[$i],$modelname);
                $list1[] = $this->deletethumb($ids[$i],$modelname);
            }
            if(count($list)==array_sum($list) && count($list1)==array_sum($list1)){
                $id = array_merge($ids);
                if($this->$modelname->destroy($id)){
                    $this->success('删除成功');
                }else{
                    $this->error('删除失败');
                }
            }else{
                $this->error('删除出现错误,请重试');
            }

        }

        if ($id) {
            $list  = $this->deletephoto($id,$modelname);
            $list1 = $this->deletethumb($id,$modelname);
            if($list && $list1){
                if ($this->$modelname->destroy($id)) {
                    $this->success('删除成功');
                } else {
                    $this->error('删除失败');
                }
            }else{
                $this->error('删除出现错误,请重试');
            }
            
        } else {
            $this->error('请选择需要删除的内容');
        }
    }

    /**
     * 删除操作(删除图片)
     * @param int   $id
     * @param array $ids
     */
    public function delete3($id, $ids,$modelname)
    {
        if($ids){           
            $i = 0;
            for(;$i<count($ids);$i++){
                $list[]  = $this->deletephoto($ids[$i],$modelname);
            }
            if(count($list)==array_sum($list)){
                $id = array_merge($ids);
                if($this->$modelname->destroy($id)){
                    $this->success('删除成功');
                }else{
                    $this->error('删除失败');
                }
            }else{
                $this->error('删除出现错误,请重试');
            }

        }

        if ($id) {
            $list  = $this->deletephoto($id,$modelname);
            if($list){
                if ($this->$modelname->destroy($id)) {
                    $this->success('删除成功');
                } else {
                    $this->error('删除失败');
                }
            }else{
                $this->error('删除出现错误,请重试');
            }
            
        } else {
            $this->error('请选择需要删除的内容');
        }
    }


     /**
     * 删除缩略图thumb
     * @return 1
     */
    public function deletethumb($id,$modelname)
    {
        //删除缩略图
        $fileImg = $this->$modelname->where('id',$id)->value('thumb');
        if($fileImg){
            if(file_exists('./'.$fileImg)){
                unlink('./'.$fileImg);
            }
        }
        return 1;
    }

    /**
     * 删除图片
     * @return 1
     */
    public function deletephoto($id,$modelname)
    {
        //删除图片集合
        $photo = $this->$modelname->where('id',$id)->value('photo');
        if($photo){
            $arr = getphoto($photo);
            //循环删除图集图片
            for($i = 0;$i<count($arr);$i++){
                if(file_exists('./'.$arr[$i])){
                    unlink('./'.$arr[$i]);
                }
            }           
        }
        return 1;
    }

}